package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@RequestMapping("system/role")
@Controller
public class RoleController extends BaseController{
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;
    @RequestMapping(value = "/list",name = "分页查询所有角色")
    //查询所有
    public String findAll(@RequestParam(name = "page",defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int pages){
        PageInfo<Role> page = roleService.findPage(getCompanyId(), pageNum, pages);
        request.setAttribute("page",page);
        return "system/role/role-list";
    }
    @RequestMapping(value = "/toAdd",name = "跳转到添加界面")
    //跳转到添加界面
    public String toAdd(){
        return "system/role/role-add";
    }
    @RequestMapping(value = "/edit",name = "添加角色数据")
    public String edit(Role role){
        if(StringUtils.isBlank(role.getId())) {
            role.setId(UUID.randomUUID().toString());
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.insert(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "跳转到编辑用户资料界面")
    //跳转到编辑用户资料界面
    public String update(String id){
        Role role = roleService.findAllById(id);
        request.setAttribute("role",role);
        return "system/role/role-add";
    }
    @RequestMapping(value = "/delete",name = "删除用户")
    //删除出用户
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/roleModule",name = "跳转到权限分配界面")
    public String roleModule(String roleid){
        Role role = roleService.findAllById(roleid);
        /*List<String> list=roleService.findMoDuleByRoleid(roleid);*/
        request.setAttribute("role",role);
        return "system/role/role-module";
    }
    //ajax返回权限列表以及角色具有的权限
    @RequestMapping(value = "/getZtreeNodes",name = "进入权限管理的界面显示")
    @ResponseBody
    public List<Map> getZtreeNodes(String roleid){
        //查询所有的模块，将数据回显回去
        List<Module> ListModule = moduleService.findAll();
        //根据roleid去re_module_role表中去查询所有的moduleid的值
        List<String> ListModuleId=roleService.findAllModuleIdByRoleid(roleid);
        Map map=null;
        List<Map> list=new ArrayList<>();
        for (Module module : ListModule) {
            map=new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            map.put("open",true);
            if (ListModuleId.contains(module.getId())){
                map.put("checked",true);
            }
            list.add(map);
        }
        //把所有的数据放在list集合中
        return list;
    }
    @RequestMapping(value = "/updateRoleModule",name = "更新权限管理")
    public String updateRoleModule(String roleid,String[] moduleIds){
        roleService.deleteModuleIdByRoleId(roleid);
        for (String moduleId : moduleIds) {
            roleService.insertModuleIdByRoleId(roleid,moduleId);
        }
        return "redirect:/system/role/list.do";
    }
}
